package com.sheng.controller;


import com.github.pagehelper.PageInfo;
import com.sheng.common.Result;
import com.sheng.common.ResultEnum;
import com.sheng.entity.User;
import com.sheng.po.user.PageUserQuery;
import com.sheng.po.user.UserInfo;
import com.sheng.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户管理 前端控制器
 * </p>
 *
 * @author 王胜
 * @since 2022 -11-19 15:56
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息相关接口")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 根据用户account查询用户信息
     *
     * @param request the request
     * @return the result
     */
    @GetMapping("/info")
    @ApiOperation("获取用户头像昵称")
    public Result<UserInfo> getUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        User user = userService.getUserInfo(token);
        UserInfo userInfo = new UserInfo(user.getAccount(),user.getImageUrl());
        return new Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), userInfo);
    }

    /**
     * 分页+模糊查询
     * @param pageUserQuery 用户信息查询PO
     * @return 返回相关用户信息
     */
    @PostMapping("/findPage")
    @ApiOperation("分页模糊查询用户信息")
    public Result getUserInfoByPage(@RequestBody PageUserQuery pageUserQuery){
        PageInfo<User> pageInfo = userService.getUserInfoByPage(pageUserQuery);
        log.info(pageInfo.toString());
        return new Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), pageInfo);
    }

    @PostMapping("/save")
    @ApiOperation("新增用户")
    public Result saveUser(@RequestBody User user){
        userService.saveUser(user);
        return new Result<>(200, "新增成功",null);
    }

    @GetMapping("/getUserById/{id}")
    @ApiOperation("根据用户艾迪获取详细信息")
    public Result getUserById(@PathVariable Integer id){
        User user = userService.getUserById(id);
        return new Result<>(200,"操作成功",user);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据用户艾迪删除")
    public Result deleteById(@PathVariable Integer id){
        Integer integer = userService.deleteById(id);
        return new Result<>(200,"删除成功",null);
    }

    @PostMapping("/update")
    @ApiOperation("更新用户信息")
    public Result updateUserInfo(@RequestBody User user){
        Integer update = userService.updateUserInfo(user);
        return new Result<>(200,"更新成功",null);
    }
}
